import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/blind_model/my_blind_list_model.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_model.dart';
import 'package:yysk_app/model/user_prop_model/my_prop_list_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/js_tool.dart';

class MinePageController extends GetxController {
  final type = 'Collection'.obs;
  final isShowRecord = RxBool(false);
  //我的藏品
  final listBlind = RxList<MyBlindListItem>([]);
  final listProp = RxList<UserPropsItem>([]);
  final listCollection = RxList<MyCollectionListItem>([]);
  final ScrollController scrollController = ScrollController();
  int pageNum = 1;
  int pageSize = 10;
  @override
  void onInit() {
    super.onInit();
    getCollectionList();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        if (type.value == 'Collection') {
          getCollectionList(loadMore: true);
        } else if (type.value == 'blindbox') {
          getBlindList(loadMore: true);
        } else {
          getPropList(loadMore: true);
        }
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  getPropList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }
    try {
      http.netFetch<UserPropsModel>(
        ApiUrl.userProps,
        params: {'pageNum': pageNum, 'pageSize': 20},
        method: Method.post,
        success: (result) {
          List<UserPropsItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            listProp.clear();
          }
          listProp.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  getBlindList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<MyBlindListModel>(
        ApiUrl.userBoxPage,
        params: {'pageNum': pageNum, 'pageSize': 20},
        method: Method.post,
        success: (result) {
          List<MyBlindListItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            listBlind.clear();
          }
          listBlind.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  getCollectionList({loadMore = false}) {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<MyCollectionListModel>(
        ProductApi.userCollection,
        params: {'pageNum': pageNum, 'pageSize': 20},
        method: Method.post,
        success: (result) {
          List<MyCollectionListItem> items = result.data?.list ?? [];
          consoleLog('${items.length}====lengt');
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            listCollection.clear();
          }
          listCollection.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  void onChange(String value) {
    // controller.modeType.value = value;
    // showToast('${value}');
    if (value == type.value) {
      return;
    }
    if (value == 'Collection') {
      getCollectionList();
      isShowRecord.value = false;
    } else if (value == 'blindbox') {
      isShowRecord.value = true;
      getBlindList();
    } else {
      isShowRecord.value = false;
      getPropList();
    }
    type.value = value;
  }
}
